package com.jhd.fuzhou.view.task;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.DateTime;
import com.jhd.fuzhou.util.GetSupervisionInspectionTaskSituation;
import com.jhd.fuzhou.view.login.MainActivity;
import com.jhd.fuzhou.view.login.MineActivity;
import com.jhd.fuzhou.vo.TaskSearch;

import net.zigin.commons.util.StringUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jhd.fuzhou.util.TimeUtil.getTimeDialog;
import static com.jhd.fuzhou.util.TimeUtil.getTimeDialog4;

public class TaskActivity extends BaseActivity {
    private Context context=this;
    @BindView(R.id.rv_status)RecyclerView rv_status;
    @BindView(R.id.rv_task)RecyclerView rv_task;
    private String[] statusList=new String[]{"未办","已办"};
    private String[] timeList=new String[]{"全部","今日","昨日","本周","本月","自定义时间"};
    private AlertDialog _alertDialog;
    private RecyclerView rv_time;//弹窗选择时间
    private LinearLayout lin_time;//如果自定义时间就显示，否则gone
    private TaskSearch taskSearch;
    private TextView tv_startTime,tv_endTime;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_task);
    }

    @Override
    protected void processLogic() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        rv_task.setLayoutManager(linearLayoutManager);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,4);
        rv_status.setLayoutManager(gridLayoutManager);
        setStatus();//显示选择状态的RecyclerView
        taskSearch=new TaskSearch();
        taskSearch.setIsHomepage("0");
        taskSearch.setIsFinish("0");
    }

    @Override
    protected void onStart() {
        super.onStart();
        getList();
    }

    public void getList(){
        GetSupervisionInspectionTaskSituation.getSupervisionInspectionTaskSituation(application,context,rv_task,taskSearch);
    }

    @OnClick({R.id.btn_sy,R.id.btn_event,R.id.btn_mine,R.id.lin_select})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_sy://底部首页
                startActivity(new Intent(context, MainActivity.class));
                finish();
                break;
            case R.id.btn_event://底部事件
                //startActivity(new Intent(context, EventActivity.class));
                //finish();
                break;
            case R.id.btn_mine://底部我的
                startActivity(new Intent(context, MineActivity.class));
                finish();
                break;
            case R.id.lin_select://顶部筛选
                dialogShow();
                break;
        }
    }

    public void setStatus(){
        StatusAdapter statusAdapter=new StatusAdapter();
        rv_status.setAdapter(statusAdapter);
    }

    //点击筛选，跳弹窗
    public void dialogShow() {
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View view = mLayoutInflater.inflate(R.layout.dialog_task, null);
        final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        _alertDialog.show();
        EditText et_search_dialog = (EditText) view.findViewById(R.id.et_search);
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);
        rv_time = (RecyclerView) view.findViewById(R.id.rv_time);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Button btn_reset = (Button) view.findViewById(R.id.btn_reset);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        LinearLayout lin_startTime = (LinearLayout) view.findViewById(R.id.lin_startTime);
        LinearLayout lin_endTime = (LinearLayout) view.findViewById(R.id.lin_endTime);
        tv_startTime = (TextView) view.findViewById(R.id.tv_startTime);
        tv_endTime = (TextView) view.findViewById(R.id.tv_endTime);
        lin_time = (LinearLayout) view.findViewById(R.id.lin_time);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,3);
        rv_time.setLayoutManager(gridLayoutManager);
        TimeAdapter statusAdapter=new TimeAdapter();
        rv_time.setAdapter(statusAdapter);
        lin_startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeDialog4(tv_startTime,context);
            }
        });
        lin_endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeDialog4(tv_endTime,context);
            }
        });

        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                taskSearch.setTaskName(null);taskSearch.setStartTime(null);taskSearch.setEndTime(null);
                setStatus();
                lin_time.setVisibility(View.GONE);
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
                String taskName=et_search_dialog.getText().toString().trim();
                taskSearch.setTaskName(StringUtil.isNullOrEmpty(taskName)?null:taskName);
                if(!tv_startTime.getText().toString().equals("请选择"))taskSearch.setStartTime(tv_startTime.getText().toString());
                if(!tv_endTime.getText().toString().equals("请选择"))taskSearch.setEndTime(tv_endTime.getText().toString());
                GetSupervisionInspectionTaskSituation.getSupervisionInspectionTaskSituation(application,context,rv_task,taskSearch);
            }
        });
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_search_dialog.setText("");
                iv_cancel.setVisibility(View.GONE);
            }
        });
        et_search_dialog.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                if(!s.toString().equals("")){
                    iv_cancel.setVisibility(View.VISIBLE);
                }else{
                    iv_cancel.setVisibility(View.GONE);
                }
            }
        });
        WindowManager.LayoutParams params = window.getAttributes() ;
        params.width =(int) (getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);
    }


    public class StatusAdapter extends RecyclerView.Adapter<StatusAdapter.ViewHolder> {
        private int checkPosition=0;
        @NonNull
        @Override
        public StatusAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radio3, parent, false);
            StatusAdapter.ViewHolder viewHolder = new StatusAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull StatusAdapter.ViewHolder holder, int position) {
            holder.rb_radio.setText(statusList[position]);
            holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkPosition=position;
                    notifyDataSetChanged();
                    //将弹窗筛选条件清空
                    taskSearch.setEndTime(null);
                    taskSearch.setStartTime(null);
                    taskSearch.setTaskName(null);
                    if(checkPosition==0){
                        taskSearch.setIsFinish("0");
                    }else if(checkPosition==1){
                        taskSearch.setIsFinish("1");
                    }
                    GetSupervisionInspectionTaskSituation.getSupervisionInspectionTaskSituation(application,context,rv_task,taskSearch);
                }
            });
            if(position==checkPosition){
                holder.rb_radio.setChecked(true);
            }else{
                holder.rb_radio.setChecked(false);
            }
        }

        @Override
        public int getItemCount() {
            return statusList.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            RadioButton rb_radio;
            public ViewHolder(View itemView) {
                super(itemView);
                this.rb_radio = itemView.findViewById(R.id.rb_radio);
            }
        }
    }
    public class TimeAdapter extends RecyclerView.Adapter<TimeAdapter.ViewHolder> {
        private int checkPosition=0;
        /*public StatusAdapter(String[] list,LinearLayout linearLayout) {
            this.list = list;
        }*/
        @NonNull
        @Override
        public TimeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radio2, parent, false);
            TimeAdapter.ViewHolder viewHolder = new TimeAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull TimeAdapter.ViewHolder holder, int position) {
            holder.rb_radio.setText(timeList[position]);
            holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkPosition=position;
                    notifyDataSetChanged();
                    if(timeList[checkPosition].equals("自定义时间")){
                        lin_time.setVisibility(View.VISIBLE);
                        taskSearch.setStartTime(null);
                        taskSearch.setEndTime(null);
                    }else{
                        lin_time.setVisibility(View.GONE);
                        if(checkPosition==0){
                            taskSearch.setStartTime(null);
                            taskSearch.setEndTime(null);
                            tv_startTime.setText("请选择");
                            tv_endTime.setText("请选择");
                        }else if(checkPosition==1){//今天
                            List<String> list=DateTime.getToday();
                            taskSearch.setStartTime(list.get(0));
                            taskSearch.setEndTime(list.get(1));
                            tv_startTime.setText("请选择");
                            tv_endTime.setText("请选择");
                        }else if(checkPosition==2){//昨日
                            List<String> list=DateTime.getYesterday();
                            taskSearch.setStartTime(list.get(0));
                            taskSearch.setEndTime(list.get(1));
                            tv_startTime.setText("请选择");
                            tv_endTime.setText("请选择");
                        }else if(checkPosition==3){//本周
                            List<String> list=DateTime.getWeek();
                            taskSearch.setStartTime(list.get(0));
                            taskSearch.setEndTime(list.get(1));
                            tv_startTime.setText("请选择");
                            tv_endTime.setText("请选择");
                        }else if(checkPosition==4){//本月
                            Date date=new Date();
                            List<String> list=DateTime.getMoon(date);
                            taskSearch.setStartTime(list.get(0));
                            taskSearch.setEndTime(list.get(1));
                            tv_startTime.setText("请选择");
                            tv_endTime.setText("请选择");
                        }
                    }
                }
            });
            if(position==checkPosition){
                holder.rb_radio.setChecked(true);
            }else{
                holder.rb_radio.setChecked(false);
            }
        }

        @Override
        public int getItemCount() {
            return timeList.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            RadioButton rb_radio;
            public ViewHolder(View itemView) {
                super(itemView);
                this.rb_radio = itemView.findViewById(R.id.rb_radio);
            }
        }
    }
}